package checkers_seperate_classes;

import java.util.ArrayList;
import java.util.List;

import checkers_seperate_classes.CheckersData.SquareContent;
import genericTree.IncorporateParent;

public class MoveNode implements IncorporateParent<MoveNode> {

	private final List<CheckersMove> moves;
	private final Double score;
	private final int moveNumber;
	int nChildreRedWinning;
	int nChildren;
	private final SquareContent player;
	
	public MoveNode(int moveNumber) {
		moves = null;
		score = null;
		player = null;
		this.moveNumber = moveNumber;
		nChildren = nChildreRedWinning= 0;
	}
	public MoveNode(CheckersMove aMove, Double aScore, SquareContent aPlayer, int moveNumber) {
		super();
		if (aMove == null) {
			moves = null;
		} else {
			moves = new ArrayList<CheckersMove>();
			moves.add(aMove);
		}
		if (aScore == null) {
			score = null;
		} else {
			score = new Double(aScore);
		}
		if (aPlayer == null) {
			player = null;
		} else {
			player = aPlayer;
		}
		this.moveNumber = moveNumber;
	}
	public MoveNode(MoveNode aThat) {
		if (aThat.moves==null) {
			this.moves = null;
		} else {
			this.moves = new ArrayList<>(aThat.moves);
		}
		if (aThat.score==null) {
			this.score = null;
		} else {
			this.score = aThat.score;
		}
		if (aThat.player == null) {
			player = null;
		} else {
			player = aThat.player;
		}
		nChildren = aThat.nChildren;
		nChildreRedWinning = aThat.nChildreRedWinning;				
		this.moveNumber = aThat.moveNumber;
	}
	
	@Override
	public MoveNode incorporateParent(MoveNode parent, MoveNode child) {
		return null;
	}

	public List<CheckersMove> getMove() {
		return moves;
	}

	@Override
	public MoveNode createNewInstance() {
		MoveNode rtn = new MoveNode(this);
		return rtn;
	}
	
	@Override
	public String toString() {
		String rtn = "";
		if (player != null) {
			rtn += player.toString()+", ";
			rtn += "turn "+getMoveNumber();
		}
		if (moves != null) {
			if (!rtn.isEmpty()) rtn += " ";
			rtn += moves.toString();
		}
		if (score != null) {
			if (!rtn.isEmpty()) rtn += ", ";
			rtn += "score "+score;
		}
		if (nChildren>0) {
			int nWins = nChildreRedWinning;		
			if (player == SquareContent.BLACK) {
				nWins = nChildren - nChildreRedWinning;
			}
			final double percentWins = ((double)nWins)/nChildren*100;
			if (!rtn.isEmpty()) rtn += " ";
			rtn += String.format("nChildren %d, nWins %d, percent wins: %5.1f", 
					nChildren, nWins, percentWins);
		}

 		return rtn;
	}
	public SquareContent getPlayer() {
		return player;
	}
	public Double getScore() {
		return score;
	}
	public void incrementNChildren() {
		this.nChildren++;
	}
	public int getNChildren() {
		return nChildren;
	}
	public void incrementChildrenWithRedWins() {
		this.nChildreRedWinning++;
	}
	public int getNRedWins() {
		return nChildreRedWinning;
	}
	public void setNRedWins(int nRedWins) {
	   this.nChildreRedWinning = nRedWins;
	}
	public int getMoveNumber() {
		return moveNumber;
	}

}
